import datetime
from typing import List, Dict
from common.utils.utils_log import AntLogger
from views.template_page_view import FormItemDto


class DataDictionaryService:
    def __init__(self):
        # 模拟内存数据存储
        self._data_store = {
            "email_server": "smtp.example.com",
            "email_port": "587",
            "max_retries": "3",
            "timeout_seconds": "30",
            "default_language": "zh_CN"
        }

    def save_or_update_common_setting(self, dict_key: str, dict_value: str, dict_desc: str = None):
        """
        保存或更新字典项
        如果键存在则更新，否则创建新条目
        """
        if dict_key in self._data_store:
            # 更新已存在的项
            old_value = self._data_store[dict_key]
            self._data_store[dict_key] = dict_value
            AntLogger.info(
                f"字典项 {dict_key} 已更新: 值={dict_value}, 旧值={old_value}")
        else:
            # 创建新项
            self._data_store[dict_key] = dict_value
            AntLogger.info(f"字典项 {dict_key} 已创建: 值={dict_value}")

    def get_all_common_settings(self):
        """
        获取所有字典项及其值
        """
        form_items = self._generate_form_items()

        # 为每个表单项检查数据存储中是否有值
        for form_item in form_items:
            if form_item.form_key in self._data_store:
                form_item.form_value = self._data_store[form_item.form_key]

        return form_items

    def get_setting_by_key(self, key: str) -> str:
        """
        通过键获取特定设置值
        """
        return self._data_store.get(key, None)

    def _generate_form_items(self) -> List[FormItemDto]:
        """
        生成表单项列表（替代原来的FormItemsKey枚举）
        """
        # 模拟生成一批表单项
        form_items = [
            FormItemDto(form_key="email_server",
                        form_desc="邮件服务器", form_value=""),
            FormItemDto(form_key="email_port",
                        form_desc="邮件端口", form_value=""),
            FormItemDto(form_key="max_retries",
                        form_desc="最大重试次数", form_value=""),
            FormItemDto(form_key="timeout_seconds",
                        form_desc="超时时间(秒)", form_value=""),
            FormItemDto(form_key="default_language",
                        form_desc="默认语言", form_value="")
        ]
        return form_items
